package cn.com.xy.sms.sdk.ui.popu.util;

import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.util.StringUtils;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.LruCache;

@SuppressLint("NewApi")
public class ResourceCacheUtil {

    //颜色值缓存
    static LruCache<String, Integer> mColorCache = new LruCache<String, Integer>(40);
    // 图片缓存
    static LruCache<String,BitmapDrawable> mImgCache = new LruCache<String,BitmapDrawable>(30);
    // 颜色图片缓存
    static LruCache<String,Drawable> mColorDrawableCache = new LruCache<String,Drawable>(60);
    public static int parseColor(String color){
        int res = -1;
        try {
            if (StringUtils.isNull(color) || color.indexOf(".") != -1) {
                LogManager.e("ResourceCacheUtil parseColor", "参数color的颜色值错误,color=",null);
                return -1;
            }
            
            Integer rc = mColorCache.get(color);
            if (rc != null) {
                res = rc;
            } else {
                res = Color.parseColor(color);
                mColorCache.put(color, res);
            }
        } catch (Throwable e) {
//            ex.printStackTrace();
            LogManager.e(Constant.TAG, "parseColor: ", e);

        }
        return res;
    }
    public static BitmapDrawable getImgDrawable(String key){
        if(key == null){
            return null;
        }
        return mImgCache.get(key);
    }
    
    public static void putImgDrawable(String key,BitmapDrawable bdw){
        if(key == null || bdw == null){
            return;
        }
        synchronized (mImgCache) {
            mImgCache.put(key, bdw);
        }
    }
    
    public static Drawable getColorDrawable(String key){
        if(key == null){
            return null;
        }
        return mColorDrawableCache.get(key);
    }
    
    public static void putColorDrawable(String key,Drawable bdw){
        if(key == null || bdw == null){
            return;
        }
        synchronized (mColorDrawableCache) {
            mColorDrawableCache.put(key, bdw);
        }
    }
    
    public static void clearCache(){
        mColorCache.evictAll();
        mColorDrawableCache.evictAll();
        mImgCache.evictAll();
    }
    
}
